<template>
  <BasicDrawer v-bind="$attrs" @register="registerDrawer" destroyOnClose  showFooter :title="getTitle" width="50%" @ok="handleSubmit">
    <BasicForm @register="registerForm">
      <template #fileList="{ model, field }">
        <a-table :columns="columns" :data-source="content" bordered size="small">
          <template #bodyCell="{ column, record }">
            <template v-if="column.key === 'action'">
              <a target="_blank" :href="record.ossPath" download>下载</a>
            </template>
          </template>
        </a-table>
      </template>
    </BasicForm>
  </BasicDrawer>
</template>
<script lang="ts" setup>
import {defineComponent, ref, computed, unref, h} from 'vue';
import { BasicForm, useForm } from '@apps/antd-admin/src/components/Form/index';
import { BasicDrawer, useDrawerInner } from '@apps/antd-admin/src/components/drawer';
import { formSchema } from './data';
import {render} from "@apps/antd-admin/src/hooks/render/renderUtils";
import {Api} from "./api";
import {Tag} from "ant-design-vue";
// 声明Emits
const emit = defineEmits(['success', 'register']);
const isUpdate = ref(true);
const content = ref([]);

const [registerForm, { resetFields, setFieldsValue,saveOrUpdate, validate }] = useForm({
  labelWidth: 90,
  schemas: formSchema,
  rowKey: 'orderInfoId',
  updateApi: Api.update,
  updateHandler:"orderInfoUpdateHandler",
  showActionButtonGroup: false,
});

const columns = [
  {
    title: '文件名',
    dataIndex: 'name',
  },
  {
    title: '文件类型',
    dataIndex: 'fileTag',
    customRender: ({ record }: Recordable) => {
       let fileTag=record.fileTag;
       if(fileTag==1){
         return '原稿'
       }
      if(fileTag==2){
        return '编译稿'
      }
      if(fileTag==3){
        return '修改意见'
      }
    },
  },
  {
    title: '操作',
    key:'action',
    dataIndex: 'ossPath',
  },
];

const [registerDrawer, { setDrawerProps, changeLoading,closeDrawer }] = useDrawerInner(async (data) => {
  await resetFields();
  setDrawerProps({ confirmLoading: false });
  isUpdate.value = !!data?.isUpdate;
  if (unref(isUpdate)) {
    content.value=data.record.fileList;
    await setFieldsValue({
      ...data.record,
    });
  }
});

const getTitle = computed(() => (!unref(isUpdate) ? '新增' : '订单报价'));

function download(record) {
  let link = document.createElement('a');
  link.style.display = 'none';
  link.href =record.ossPath
  document.body.appendChild(link);
  link.click();
}

async function handleSubmit() {
  try {
    changeLoading(true);
    await saveOrUpdate(emit);
    closeDrawer();
  } finally {
    changeLoading(false);
  }
}
</script>
